package com.zeekr.orm.rabbitmq;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author 吴文杰
 * @version 1.0
 * @since 2024/7/11 6:06
 */
@Component
@Slf4j
public class UserMessageConsumer {

  @RabbitListener(queues = {"config-queue"}, ackMode = "MANUAL")
  public void receiveMessage(String message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) {
    // getInfo(message);
    // try {
    //   channel.basicAck(deliveryTag, false);
    // } catch (IOException e) {
    //   e.printStackTrace();
    // }
  }

  @RabbitListener(queues = {"user-service-queue"})
  public void userServiceMessage(String message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) {
    getInfo(message);
    try {
      channel.basicAck(deliveryTag, false);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  private static void getInfo(String message) {
    log.info("receiveMessage message = {}", message);
  }

  @RabbitListener(queues = {"test-queue"})
  public void testMessage(String message) {
    // getInfo(message);
  }

}
